package com.qiyun.dinghuohui.utils;

import android.content.Context;
import android.content.Intent;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.qiyun.dinghuohui.ApplicationDataHolder;
import com.qiyun.dinghuohui.activity.DialogActivity;
import com.qiyun.dinghuohui.common.ClientCookie;
import com.qiyun.dinghuohui.common.CommonDataSets;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import org.apache.http.util.EntityUtils;
import org.josql.Query;
import org.josql.QueryExecutionException;
import org.josql.QueryParseException;
import org.josql.QueryResults;
import org.json.JSONObject;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;

public class Utils {
	public static boolean isAdapterEmpty(ListView listView) {
		if(listView.getAdapter().isEmpty()) {
			return true;
		} 
		
		return false;
	}
	
	public static boolean isTextViewEmpty(TextView textView) {
		if("".equals(textView.getText().toString().trim())) {
			return true;
		}
		
		return false;
	}
	
	public static boolean isSpinnerTextViewEmpty(TextView textView) {
		if("选择..".equals(textView.getText().toString().trim())) {
			return true;
		}
		
		return false;
	}
	
	public static void makeToastInfo(Context context, String msg) {
		Toast.makeText(context, msg, Toast.LENGTH_LONG).show();
	}
	/*
	public static Intent makeShowPicImageIntentOld(Context ctx, String style_code) {
		Intent intent = new Intent(ctx, DialogActivity.class);
		intent.putExtra("imageUrl", "/goods/img/" + style_code + "_1.jpg");
		intent.putExtra("style_code", style_code);
		return intent;
	}
	*/
	public static Intent makeShowPicImageIntent(Context ctx, String sPicCode, int imageCount,String[] imagePath) {
		Intent intent = new Intent(ctx, DialogActivity.class);
		intent.putExtra("imageUrl", "/goods/img/" + sPicCode);
		intent.putExtra("piccode", sPicCode);
		intent.putExtra("imageCount", imageCount);
		intent.putExtra("imageType", 0);
		intent.putExtra("imagePath", imagePath);
		return intent;
	}
	
	public static Intent makeShowPairPicImageIntent(Context ctx, String pair_id) {
		Intent intent = new Intent(ctx, DialogActivity.class);
		intent.putExtra("imageUrl", "/goods/pair/" + pair_id + ".jpg");
		intent.putExtra("piccode", "pair"+pair_id);
		intent.putExtra("imageCount", 1);
		intent.putExtra("imageType", 1);
		return intent;
	}
	
	public static Intent makeShowPairImageIntent(Context ctx, String pairCode) {
		Intent intent = new Intent(ctx, DialogActivity.class);
		intent.putExtra("piccode", pairCode);
		intent.putExtra("imageUrl", CommonDataSets.CLIENT_STRONGPATH+"/"+pairCode+".jpg");
		intent.putExtra("imageCount", 1);
		intent.putExtra("imageType", 1);
		return intent;
	}
	
	public static Intent makeShowPairPicImageIntent(Context ctx, String pair_id, int imageCount) {
		Intent intent = new Intent(ctx, DialogActivity.class);
		intent.putExtra("imageUrl", "/goods/pair/" + pair_id);
		intent.putExtra("piccode", "pair"+pair_id);
		intent.putExtra("imageCount", imageCount);
		intent.putExtra("imageType", 1);
		return intent;
	}
	
	private static final String JSON_DATA = "jsonData";
	private static final int mTimeoutConnection = 20000;
	private static final int mTimeoutSocket = 30000;
	public static JSONObject sendSync(String subUrl, JSONObject parameters) throws Exception {
		DefaultHttpClient mHttpClient = null;
		try {
			List<NameValuePair> formParams = new ArrayList<NameValuePair>();
			formParams.add(new BasicNameValuePair(JSON_DATA, parameters.toString()));
			HttpEntity entity = new UrlEncodedFormEntity(formParams, "UTF-8");
			HttpResponse httpResponse = null;
			HttpPost request = new HttpPost(ClientCookie.getServerUrl() + subUrl);
			request.setEntity(entity);
			
			HttpParams httpParameters = new BasicHttpParams();
			HttpConnectionParams.setConnectionTimeout(httpParameters, mTimeoutConnection);
			HttpConnectionParams.setSoTimeout(httpParameters, mTimeoutSocket);
			mHttpClient = new DefaultHttpClient(httpParameters);
			httpResponse = mHttpClient.execute(request);
			
			if(httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
				String retSrc = EntityUtils.toString(httpResponse.getEntity());
				return new JSONObject(retSrc);
			} else {
				throw new Exception("********< error >********: server return code:" + httpResponse.getStatusLine().getStatusCode());
			}
		} catch (Exception e) {
			e.printStackTrace();
			throw new Exception("error"); 
		}
	}
	
	//wifi 
	public static String intToIp(int i) {
		return ( i & 0xFF)+ "." +((i >> 8 ) & 0xFF)+ "." + ((i >> 16 ) & 0xFF)+ "." +((i >> 24 ) & 0xFF ) ;
	}
	//检测获得的IP地址
	public static boolean checkHaveIpAddress(ApplicationDataHolder adh){
		boolean bSucc=false;
		int waittimecount=0;
		while (true){
			//CheckNetworkActivity();
			adh.setOnline(adh.getWifiAdmin().getIPAddress()!=0);
			if (adh.isOnline()){
				bSucc=true;
				try {
					Thread.sleep(2000);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
				break;
			}
			waittimecount++;
			if (waittimecount>60){
				adh.wifiLastSsid="";
				bSucc=false;
				break;
			}
			try {
				Thread.sleep(1000);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
		return bSucc;
	}
	
	public static double getFloatPointAndDiv(double sub,double allcount,int pointCount){
		double sdivvalue=0;
		if (allcount>0){
			sdivvalue=sub/allcount;
			sdivvalue=getFloatPointData(sdivvalue,pointCount);
		}
		return sdivvalue;
	}
	public static double getFloatPointData(double inF,int pointCount){
		String sTemp="#";
		long iTemp=Math.round(inF);
		String intvalueTemp=String.valueOf(iTemp);
		while (sTemp.length()<intvalueTemp.length()){
			sTemp=sTemp+"#";
		}
		if (pointCount>0){
			sTemp=sTemp+".";
		}
		while (sTemp.length()<pointCount+intvalueTemp.length()+1){
			sTemp=sTemp+"0";
		}
		DecimalFormat retFloat  = new DecimalFormat(sTemp); 
		String retTemp=retFloat.format(inF);
		double ret=Double.parseDouble(retTemp);
		return ret;
	}
	public static String FloatToStrLength(int length,double inF){
		String sTemp=""+inF;
		if (sTemp.length()>length){
			sTemp=sTemp.substring(0, length);
		}
		return sTemp;
	}
	
	public static JSONObject selectMiniCount(List<JSONObject> source, String sqlWhere) {
		if (source == null)
			return null;

		Query q = new Query();

		// Parse the SQL you are going to use.
		try {
			q.parse("SELECT * FROM org.json.JSONObject WHERE " + sqlWhere);
			QueryResults qr = q.execute(source);
			if(qr.getResults()==null ||qr.getResults().size()==0){
				return null;
			}else{
				return (JSONObject) qr.getResults().get(0);
			}
		} catch (QueryParseException e) {
			e.printStackTrace();
		} catch (QueryExecutionException e) {
			e.printStackTrace();
		}
		return null;
	}
	
}
